/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * File Name          : freertos.c
 * Description        : Code for freertos applications
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "lvgl.h"
#include "i2c.h"
#include "FT6236.h"
#include "usart.h"
#include "cmsis_os2.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
extern I2C_HandleTypeDef hi2c1;
extern _m_tp_dev tp_dev;
extern UART_HandleTypeDef huart1;
extern osMutexId_t lv_mutexHandle;
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for led2Task */
osThreadId_t led2TaskHandle;
const osThreadAttr_t led2Task_attributes = {
  .name = "led2Task",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for led3Task */
osThreadId_t led3TaskHandle;
const osThreadAttr_t led3Task_attributes = {
  .name = "led3Task",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for lcdTask */
osThreadId_t lcdTaskHandle;
const osThreadAttr_t lcdTask_attributes = {
  .name = "lcdTask",
  .stack_size = 2048 * 4,
  .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for lcdDisplayTask */
osThreadId_t lcdDisplayTaskHandle;
const osThreadAttr_t lcdDisplayTask_attributes = {
  .name = "lcdDisplayTask",
  .stack_size = 2048 * 4,
  .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for flashTask */
osThreadId_t flashTaskHandle;
const osThreadAttr_t flashTask_attributes = {
  .name = "flashTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for tpTask */
osThreadId_t tpTaskHandle;
const osThreadAttr_t tpTask_attributes = {
  .name = "tpTask",
  .stack_size = 512 * 4,
  .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for lv_mutex */
osMutexId_t lv_mutexHandle;
const osMutexAttr_t lv_mutex_attributes = {
  .name = "lv_mutex"
};
/* Definitions for touchPressedBinarySem */
osSemaphoreId_t touchPressedBinarySemHandle;
const osSemaphoreAttr_t touchPressedBinarySem_attributes = {
  .name = "touchPressedBinarySem"
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);
void led2TaskFunc(void *argument);
void led3TaskFunc(void *argument);
void lcdTaskFunc(void *argument);
void lcdDisplayTaskFunc(void *argument);
void flashTaskFunc(void *argument);
void tpTaskFunc(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* Hook prototypes */
void vApplicationIdleHook(void);

/* USER CODE BEGIN 2 */
void vApplicationIdleHook(void) {
	lv_tick_inc(1);
}
/* USER CODE END 2 */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */
  /* Create the mutex(es) */
  /* creation of lv_mutex */
  lv_mutexHandle = osMutexNew(&lv_mutex_attributes);

  /* USER CODE BEGIN RTOS_MUTEX */
	/* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* Create the semaphores(s) */
  /* creation of touchPressedBinarySem */
  touchPressedBinarySemHandle = osSemaphoreNew(1, 0, &touchPressedBinarySem_attributes);

  /* USER CODE BEGIN RTOS_SEMAPHORES */
	/* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
	/* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
	/* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* creation of led2Task */
  led2TaskHandle = osThreadNew(led2TaskFunc, NULL, &led2Task_attributes);

  /* creation of led3Task */
  led3TaskHandle = osThreadNew(led3TaskFunc, NULL, &led3Task_attributes);

  /* creation of lcdTask */
  lcdTaskHandle = osThreadNew(lcdTaskFunc, NULL, &lcdTask_attributes);

  /* creation of lcdDisplayTask */
  lcdDisplayTaskHandle = osThreadNew(lcdDisplayTaskFunc, NULL, &lcdDisplayTask_attributes);

  /* creation of flashTask */
  flashTaskHandle = osThreadNew(flashTaskFunc, NULL, &flashTask_attributes);

  /* creation of tpTask */
  tpTaskHandle = osThreadNew(tpTaskFunc, NULL, &tpTask_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
	/* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
	/* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
 * @brief  Function implementing the defaultTask thread.
 * @param  argument: Not used
 * @retval None
 */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
	/* Infinite loop */
	for (;;) {
		osDelay(1);
	}
  /* USER CODE END StartDefaultTask */
}

/* USER CODE BEGIN Header_led2TaskFunc */
/**
 * @brief Function implementing the led2Task thread.
 * @param argument: Not used
 * @retval None
 */
/* USER CODE END Header_led2TaskFunc */
void led2TaskFunc(void *argument)
{
  /* USER CODE BEGIN led2TaskFunc */
	/* Infinite loop */
	for (;;) {
		HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
//		HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
//		HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7);
		osDelay(200);
	}
  /* USER CODE END led2TaskFunc */
}

/* USER CODE BEGIN Header_led3TaskFunc */
/**
 * @brief Function implementing the led3Task thread.
 * @param argument: Not used
 * @retval None
 */
/* USER CODE END Header_led3TaskFunc */
void led3TaskFunc(void *argument)
{
  /* USER CODE BEGIN led3TaskFunc */
	/* Infinite loop */
	for (;;) {
		HAL_GPIO_TogglePin(LED3_GPIO_Port, LED3_Pin);
		osDelay(500);
	}
  /* USER CODE END led3TaskFunc */
}

/* USER CODE BEGIN Header_lcdTaskFunc */
/**
 * @brief Function implementing the lcdTask thread.
 * @param argument: Not used
 * @retval None
 */


/* USER CODE END Header_lcdTaskFunc */
void lcdTaskFunc(void *argument)
{
  /* USER CODE BEGIN lcdTaskFunc */
	taskENTER_CRITICAL();
	lv_init();
	lv_port_disp_init();
	lv_port_indev_init();
	taskEXIT_CRITICAL();
	vTaskSuspend(lcdTaskHandle);
	/* Infinite loop */
	for (;;) {
		if (lv_mutex_lock(lv_mutexHandle) == LV_RESULT_OK) {
			lv_timer_handler();
		} else {
			printf("get mutex lock Fail!");
		}
		lv_mutex_unlock(lv_mutexHandle);
		osDelay(1);
	}
  /* USER CODE END lcdTaskFunc */
}

/* USER CODE BEGIN Header_lcdDisplayTaskFunc */
/**
 * @brief Function implementing the lcdDisplayTask thread.
 * @param argument: Not used
 * @retval None
 */
/* USER CODE END Header_lcdDisplayTaskFunc */
void lcdDisplayTaskFunc(void *argument)
{
  /* USER CODE BEGIN lcdDisplayTaskFunc */
	/* Infinite loop */
	taskENTER_CRITICAL();
	lv_demo_widgets();
//    lv_obj_t * spinner = lv_spinner_create(lv_screen_active());
//    lv_obj_set_size(spinner, 100, 100);
//    lv_obj_align(spinner, LV_ALIGN_TOP_RIGHT, 0, 0);
//    lv_spinner_set_anim_params(spinner, 10000, 300);
//	lv_example_spinner_1();
//	lv_example_keyboard_2();
	taskEXIT_CRITICAL();
	vTaskResume(lcdTaskHandle);
	for (;;) {
		osDelay(1);
	}
  /* USER CODE END lcdDisplayTaskFunc */
}

/* USER CODE BEGIN Header_flashTaskFunc */
/**
* @brief Function implementing the flashTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_flashTaskFunc */
void flashTaskFunc(void *argument)
{
  /* USER CODE BEGIN flashTaskFunc */
	osDelay(5000);
//    W25QXX_Erase_Chip();
    uint32_t addr = 0x000001;
  /* Infinite loop */
  for(;;)
  {

//    int8_t data[5] ={1,2,3,4,5};
//    W25QXX_Write(data, &addr, sizeof(data));
	  //    memset(data,0,sizeof(data));
//    int8_t pBuffer[5] ={0};
//    W25QXX_Read(pBuffer, &addr, 5);
//    printf("pdata = %s",pBuffer);
//    memset(pBuffer,0,sizeof(pBuffer));
    osDelay(5000);
  }
  /* USER CODE END flashTaskFunc */
}

/* USER CODE BEGIN Header_tpTaskFunc */
/**
* @brief Function implementing the tpTask thread.
* @param argument: Not used
* @retval None
*/

/* USER CODE END Header_tpTaskFunc */
void tpTaskFunc(void *argument)
{
  /* USER CODE BEGIN tpTaskFunc */
	/* Infinite loop */
	for (;;) {
//	osThreadSuspend(tpTaskHandle);
		if (touchPressedScan) {
			osDelay(20);
			FT6236_Scan();
//			for (int8_t i = 0; i < 5; i++) {
//				if (tp_dev.sta & (1 << i)) {		//读取触摸点坐�??? //被按下则读取对应触摸点坐标数�???
//					uint16_t x = tp_dev.x[i];
//					uint16_t y = tp_dev.y[i];
//					printf("x[%d] = %d ; y[%d] = %d \r\n", i, x, i, y);
//				}
//			}
			touchPressedScan = 0;
		}
		osDelay(10);
	}
  /* USER CODE END tpTaskFunc */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  if (CTP_INT_Pin == GPIO_Pin) {
	  // 没有信号量，就给释放�??�??
	if (!(xSemaphoreTakeFromISR(touchPressedBinarySemHandle,0) == pdPASS)) {
		xSemaphoreGiveFromISR(touchPressedBinarySemHandle, NULL);
	}
	if (!touchPressedScan) {
		touchPressedScan = 1;
	}
	printf("int scan stask ... \r\n");
  }
}
/* USER CODE END Application */

